home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / julin105.zip / JULDEMO.C < prev    next >
C/C++ Source or Header  |  1993-08-02  |  5KB  |  222 lines

  1.  
  2. /********************************************************************
  3.  *                                                                  *
  4.  *     JULIAN DATE LIBRARY DEMO (c)Copyright 1992 by CalcShop Inc.    *
  5.  *                      All rights reserved.                          *
  6.  *                                                                  *
  7.  *                 JULIAN v1.05 d08/02/93                           *
  8.  *                                                                  *
  9.  *                         Robert Emmons                              *
  10.  *                         CalcShop Inc.                              *
  11.  *                          P.O Box 1231                              *
  12.  *                     W. Caldwell, NJ  07007                         *
  13.  *             Phones: (201)228-9139 or (800)466-3469                 *
  14.  *                                                                  *
  15.  ********************************************************************/
  16.  
  17. #include <stdio.h>
  18. #include <conio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <dos.h>
  22. #include <time.h>
  23. #include <ctype.h>
  24. #include "juldef.h"
  25.  
  26.  
  27. int waitkey(void);
  28. int timestamp(FILE *fp);
  29. int makedate(char *, int);
  30.  
  31.  
  32.  
  33. main(int argc, char *argv[] /* ,char *env[] */)
  34. {
  35.     char str[82];            /* allows for 80 chars + \n + \0 */
  36.     int done = FALSE, pass = 1, autorun = FALSE;
  37.     int nearyear = 2000;
  38.     int weekday;
  39.     long jday;
  40.     double jear;
  41.     GDATE gdate, jdaychkdate, jearchkdate;
  42.  
  43.     fputs("\n", stdout);
  44.     timestamp(stdout);
  45.     fprintf(stdout, "     nearyear = %-5d", nearyear);
  46.     fputs("\nOutput shows: \"Stardate\", dayofweek = mm/dd/yy (if possible) = mm/dd/yyyy", stdout);
  47.  
  48.     if(argc > 1 && toupper(*argv[1]) == 'M')
  49.         autorun = FALSE;
  50.     else{
  51.         autorun = TRUE;
  52.     }
  53.     done = FALSE;
  54.     pass = 1;            /* counter for automatic operation */
  55.     while(done != TRUE)
  56.     {
  57.  
  58.         /* input a Gregorian date string from the keyboard or
  59.            the preprogrammed list
  60.         */
  61.         if(autorun == TRUE)
  62.             done = makedate(str, pass);
  63.         else{
  64.             fputs("\n\nEnter date (mm/dd/yyyy): ", stdout);
  65.             fgets(str, 80, stdin);
  66.             done = TRUE;
  67.         }
  68.  
  69.         /* use various JULIAN LIBRARY conversion and other functions */
  70.         gdate = mmddyyyytogreg(str, nearyear);
  71.         jday = gregtojday(gdate);
  72.         jear = gregtojear(gdate);
  73.         weekday = dayofweek(jday);
  74.         jdaychkdate = jdaytogreg(jday);
  75.         jearchkdate = jeartogreg(jear);
  76.  
  77.         /* Display the input and converted dates */
  78.         if(autorun == TRUE){
  79.             fputs("\n          Entered date = ", stdout);
  80.             fputs(str, stdout);
  81.         }
  82.         if(jday > 0){
  83.             fprintf(stdout, "   %6ld, %8.3lf, %d = ", jday, jear, weekday);
  84.             *str = '\x0';
  85.             gregtommddyyyy(str, jdaychkdate, nearyear);
  86.             fprintf(stdout, "%-10s = ", str);
  87.             *str = '\x0';
  88.             gregtommddyyyy(str, jearchkdate, FALSE);
  89.             fputs(str, stdout);
  90.         }
  91.         else
  92.             fputs("\nSorry: bad date entry.", stdout);
  93.  
  94.  
  95.         /* decide whether or not to exit the demo */
  96.         if(done == TRUE){
  97.             autorun = FALSE;
  98.             fputs("\nEnter another date (Y/N)? Y", stdout);
  99.             done = waitkey();
  100.             if(toupper(done) == 'N')
  101.                 done = TRUE;
  102.             else{
  103.                 done = FALSE;
  104.                 pass = 0;
  105.             }
  106.         }
  107.         ++pass;
  108.     }
  109.     fputs("\nGoodbye!", stdout);
  110.     return(FALSE);
  111. }
  112.  
  113.  
  114. int makedate(char *str, int pass)
  115. {
  116.     switch(pass){
  117.         case 1:
  118.             strcpy(str, "12/31/0001\n");
  119.             pass = FALSE;
  120.             break;
  121.         case 2:
  122.             strcpy(str, "01/01/0002\n");
  123.             pass = FALSE;
  124.             break;
  125.         case 3:
  126.             strcpy(str, "12/31/0003\n");
  127.             pass = FALSE;
  128.             break;
  129.         case 4:
  130.             strcpy(str, "01/01/0004\n");
  131.             pass = FALSE;
  132.             break;
  133.         case 5:
  134.             strcpy(str, "12/31/0004\n");
  135.             pass = FALSE;
  136.             break;
  137.         case 6:
  138.             strcpy(str, "01/01/0005\n");
  139.             pass = FALSE;
  140.             break;
  141.         case 7:
  142.             strcpy(str, "12/31/0399\n");
  143.             pass = FALSE;
  144.             break;
  145.         case 8:
  146.             strcpy(str, "01/01/0400\n");
  147.             pass = FALSE;
  148.             break;
  149.         case 9:
  150.             strcpy(str, "12/31/0400\n");
  151.             pass = FALSE;
  152.             break;
  153.         case 10:
  154.             strcpy(str, "01/01/0401\n");
  155.             pass = FALSE;
  156.             break;
  157.         case 11:
  158.             strcpy(str, "12/31/49\n");
  159.             pass = FALSE;
  160.             break;
  161.         case 12:
  162.             strcpy(str, "01/01/50\n");
  163.             pass = FALSE;
  164.             break;
  165.         case 13:
  166.             strcpy(str, "12/31/51\n");
  167.             pass = FALSE;
  168.             break;
  169.         case 14:
  170.             strcpy(str, "01/01/52\n");
  171.             pass = FALSE;
  172.             break;
  173.         case 15:
  174.             strcpy(str, "12/31/91\n");
  175.             pass = FALSE;
  176.             break;
  177.         case 16:
  178.             strcpy(str, "01/01/92\n");
  179.             pass = FALSE;
  180.             break;
  181.         case 17:
  182.             strcpy(str, "12/31/04\n");
  183.             pass = FALSE;
  184.             break;
  185.         case 18:
  186.             strcpy(str, "01/01/05\n");
  187.             pass = FALSE;
  188.             break;
  189.         default:
  190.             strcpy(str, "01/01/0001\n");
  191.             pass = TRUE;
  192.             break;
  193.     }
  194.     return pass;
  195. }
  196.  
  197.  
  198. int waitkey(void)
  199. {
  200.     int chr;
  201.     return(!(chr = getch())? getch() + 128: chr);
  202. }
  203.  
  204.  
  205. int timestamp(FILE *fp)
  206. {
  207.     struct time tim;
  208.     struct date dat;
  209.  
  210.     gettime(&tim);
  211.     getdate(&dat);
  212.     fprintf
  213.     (    fp,
  214.         "time stamp: %d/%d/%d %2d:%02d:%02d",
  215.         dat.da_mon, dat.da_day, dat.da_year - 1900,
  216.         tim.ti_hour, tim.ti_min, tim.ti_sec
  217.     );
  218.     return FALSE;
  219. }
  220.  
  221.  
  222.